﻿using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WPFStudyPrismRegion.Views;

namespace WPFStudyPrismRegion.ViewModels
{
    public class MainViewModel : BindableBase
    {
        private readonly IRegionManager regionManager;
        private IRegionNavigationJournal journal;
        private IDialogService dialogService;
        public DelegateCommand<string> OpenCommand { get; private set; }
        public DelegateCommand BackCommand { get; private set; }
        public DelegateCommand<string> DialogCommand { get; private set; }

        public MainViewModel(IRegionManager regionManager, IDialogService dialogService)
        {
            OpenCommand = new DelegateCommand<string>(Open);
            BackCommand = new DelegateCommand(Back);
            DialogCommand = new DelegateCommand<string>(ShowDialog);
            this.regionManager = regionManager;
            this.dialogService = dialogService;
        }

        private void ShowDialog(string obj)
        {
            DialogParameters keys = new DialogParameters();
            keys.Add("Title", "测试弹窗!");
            dialogService.ShowDialog(obj, keys, callback =>
            {
                if (callback.Result == ButtonResult.OK)
                {
                    string message = callback.Parameters.GetValue<string>("Value");
                }
            });
        }

        private void Back()
        {
            if (journal.CanGoBack)
                journal.GoBack();

        }

        private void Open(string obj)
        {
            //NavigationParameters parameters = new NavigationParameters();
            //parameters.Add("Title", "Hello!");
            //regionManager.RequestNavigate("ContentRegion", obj, parameters);
            //return;
            NavigationParameters keys = new NavigationParameters();
            keys.Add("Title", "Hello!");
            regionManager.Regions["ContentRegion"].RequestNavigate(obj, callback =>
            {
                journal = callback.Context.NavigationService.Journal;
            }, keys);
        }
    }

}
